package com.yima.baseproj.utils.excel;

import com.yima.baseproj.module.app.equipment_entry.EquipmentEntryEntity;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class POIUtils {
    /**
     * Excel 解析成数据集合
     *
     * @return
     */
    public static List<EquipmentEntryEntity> excel2Employee(MultipartFile file) {
        List<EquipmentEntryEntity> list = new ArrayList<>();
        EquipmentEntryEntity equipmentEntryEntity = null;
        try {
            //1. 创建一个 workbook 对象
            HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
            //2. 获取 workbook 中表单的数量
            int numberOfSheets = workbook.getNumberOfSheets();
            for (int i = 0; i < numberOfSheets; i++) {
                //3. 获取表单
                HSSFSheet sheet = workbook.getSheetAt(i);
                //4. 获取表单中的行数
                int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
                for (int j = 0; j < physicalNumberOfRows; j++) {
                    //5. 跳过标题行
                    if (j == 0) {
                        continue;//跳过标题行
                    }
                    //6. 获取行
                    HSSFRow row = sheet.getRow(j);
                    if (row == null) {
                        continue;//防止数据中间有空行
                    }
                    //7. 获取列数
                    int physicalNumberOfCells = row.getPhysicalNumberOfCells();
                    equipmentEntryEntity = new EquipmentEntryEntity();
                    for (int k = 0; k < physicalNumberOfCells; k++) {
                        HSSFCell cell = row.getCell(k);
                        switch (cell.getCellType()) {
                            // 类型是 String 进入此 case 块
                            case STRING:
                                String cellValue = cell.getStringCellValue();
                                switch (k) {
                                    case 1:
                                        equipmentEntryEntity.setDeviceNo(cellValue);
                                        break;
//                                    case 2:
//                                        employee.setWorkID(cellValue);
//                                        break;
//                                    case 3:
//                                        employee.setGender(cellValue);
//                                        break;
//                                    case 5:
//                                        employee.setIdCard(cellValue);
//                                        break;
//                                    case 6:
//                                        employee.setWedlock(cellValue);
//                                        break;
//                                    case 7:
//                                        employee.setNationId(cellValue);
//                                        break;
//                                    case 8:
//                                        employee.setNativePlace(cellValue);
//                                        break;
//                                    case 9:
//                                        employee.setPoliticId(cellValue);
//                                        break;
//                                    case 10:
//                                        employee.setPhone(cellValue);
//                                        break;
//                                    case 11:
//                                        employee.setAddress(cellValue);
//                                        break;
//                                    case 12:
//                                        employee.setDepartmentId(cellValue);
//                                        break;
//                                    case 13:
//                                        employee.setJobLevelId(cellValue);
//                                        break;
//                                    case 14:
//                                        employee.setPosId(cellValue);
//                                        break;
//                                    case 15:
//                                        employee.setEngageForm(cellValue);
//                                        break;
//                                    case 16:
//                                        employee.setTiptopDegree(cellValue);
//                                        break;
//                                    case 17:
//                                        employee.setSpecialty(cellValue);
//                                        break;
//                                    case 18:
//                                        employee.setSchool(cellValue);
//                                        break;
//                                    case 20:
//                                        employee.setWorkState(cellValue);
//                                        break;
//                                    case 21:
//                                        employee.setEmail(cellValue);
//                                        break;
                                }
                                break;
                            // 类型是 Date或者数字 进入此 case 块
//                            default: {
//                                switch (k) {
//                                    case 4:
//                                        equipmentEntryEntity.setBirthday(cell.getDateCellValue());
//                                        break;
//                                    case 19:
//                                        employee.setBeginDate(cell.getDateCellValue());
//                                        break;
//                                    case 23:
//                                        employee.setBeginContract(cell.getDateCellValue());
//                                        break;
//                                    case 24:
//                                        employee.setEndContract(cell.getDateCellValue());
//                                        break;
//                                    case 22:
//                                        employee.setContractTerm(cell.getNumericCellValue());
//                                        break;
//                                    case 25:
//                                        employee.setConversionTime(cell.getDateCellValue());
//                                        break;
//                                }
//                            }
//                            break;
                        }
                    }
                    // 最后将解析后的数据添加到员工集合中
                    list.add(equipmentEntryEntity);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

}
